// @ts-ignore
/* eslint-disable */
import { message } from "antd";
import TokenManager from "@/token";
import { request } from "@umijs/max";
import { postRefreshToken } from "../core/api";
import type { API } from "@/types/api"
// 公共Token验证方法
async function validateToken(navigate: any): Promise<boolean> {
    const accessToken = TokenManager.getAccessToken();
    const isInvalidAccessToken = [
        null,
        "",
        " [object Promise]",
        "null",
    ].includes(accessToken);
    if (!isInvalidAccessToken) {
        const refreshToken = TokenManager.getRefreshToken();
        const isInvalidRefreshToken = [
            null,
            "",
            " [object Promise]",
            "null",
        ].includes(refreshToken);
        if (isInvalidRefreshToken) {
            console.log("refresh_token过期！");
            // navigate("/login");
            window.location.reload();
            await postRefreshToken(navigate);
        }
        return false;
    }
    return true;
}


/** 获取当前的用户信息 POST /api/core/system/wfs */
export async function postWfs(
    body: API.PostWfsParam,
    node_id: string,
    options?: { [key: string]: any },
    navigate?: any,
) {
    let result;
    const access_token = TokenManager.getAccessToken();
    const isInvalidAccessToken = [null, "", " [object Promise]"].includes(
        access_token,
    );
    if (isInvalidAccessToken) {
        const refresh_token = TokenManager.getRefreshToken();
        const isInvalidRefreshToken = [null, "", " [object Promise]"].includes(
            refresh_token,
        );
        if (isInvalidRefreshToken) {
            console.log("refresh_token过期！");
            navigate("/login");
            window.location.reload();
            return null;
        } else {
            postRefreshToken(navigate);
            return null;
        }
    }
    await request<{
        data: API.CurrentUserParam;
    }>(`/api/core/system/wfs?node_id=${node_id}`, {
        method: "POST",
        headers: {
            "Content-Type": "application/json",
            Authorization: "Bearer " + TokenManager.getAccessToken(),
        },
        data: body,
    })
        .then((res) => {
            result = res;
        })
        .catch((err) => {
            result = err;
        });
    return result;
}

export async function postCheckChooseFile(
    body: API.PostCheckChooseFileParam,
    node_id: string,
    navigate: any,
): Promise<API.PostCheckChooseFileResult> {
    await validateToken(navigate);
    return await request<API.PostRealogResult>(
        `/api/apps/hosting/check_choose_file` +
            `?node_id=${node_id}`,
        {
            method: "POST",
            headers: {
                "Content-Type": "application/json",
                Authorization: "Bearer " + TokenManager.getAccessToken(),
            },
            data: body,
        },
    )
        .then((res: API.PostRealogResult) => {
            return res;
        })
        .catch(async (error: any) => {
            if (error.response.data.status_code === "EXPIRED_TOKEN") {
                console.log("触发token刷新！");
                await postRefreshToken(navigate);
                return await postCheckChooseFile(body, node_id, navigate);
            } else {
                message.error(error.response.data.message.file_path);
                return error.response.data;
            }
        });
}